package com.tjp.eight.ui.fragment.subdetails;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.RxBus;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.sub.SubEvent;
import com.tjp.eight.ui.fragment.sub.SubItemModel;
import com.tjp.eight.ui.fragment.sub.SubPModel;
import com.tjp.eight.ui.fragment.sub.SubResultModel;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.DialogSendingComment;
import com.tjp.eight.views.LogoutDailog;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;
import com.tjp.eight.views.refresh.PullRefreshLayout;

/**
 * Created by Administrator on 2017/2/27.
 */
public class FragmentSubDetail extends BaseFragment<SubDetailPresenter> implements SubDetailView {
    private RecyclerView rv_sub_detail;
    private String caid = "";
    private SubDetailAdapter subDetailAdapter;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private View footView;
    private LinearLayoutManager linearLayoutManager;
    private String currentName = "";
    private String currentPath = "";
    private int cPage = 1;
    private DialogSendingComment loadingDialog;
    private LogoutDailog dialogLogin;
//    private PullRefreshLayout pullRefreshLayout;

    @Override
    protected SubDetailPresenter createPresenter() {
        return new SubDetailPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_sub_detail, container, false);
        }
        caid = getActivity().getIntent().getStringExtra("caid");
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getJson());
    }

    @Override
    public void loadData(SubDetailResultModel model) {
        if (model == null) return;
        if (IsSuccessUtils.isSuccess(model.result)) {
            subDetailAdapter.addData(model);
            if (subDetailAdapter.getItemCount() >= model.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        } else {
            ToatUtils.toastCommentFail(getContext(), model.msg);
        }
    }

    @Override
    public void loadFail(String msg) {
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    @Override
    public void loadMore(SubDetailResultModel model) {
        if (model == null) {
            headerViewRecyclerAdapter.removeFootByIndex(0);
            return;
        }
        if (IsSuccessUtils.isSuccess(model.result)) {
            subDetailAdapter.addMoreData(model.data);
            if (subDetailAdapter.getItemCount() >= model.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        } else {
            ToatUtils.toastCommentFail(getContext(), model.msg);
        }
    }

    @Override
    public void subSuccess(SubResultModel model) {
        dismissLoading();
        if (model == null) return;
        if (IsSuccessUtils.isSuccess(model.result)) {
            SubItemModel.ItemModel itemModel = new SubItemModel().getItemModel();
            itemModel.caid = caid;
            itemModel.name = currentName;
            itemModel.cover_path = currentPath;
            itemModel.subscibe = model.subscibe;
            itemModel.if_new = 0;
            SubEvent subEvent = new SubEvent();
            subEvent.if_subscibe = model.if_subscibe;
            subEvent.model = itemModel;
            RxBus.getInstance().post(subEvent);
            subDetailAdapter.notifyIsSub(model.if_subscibe);
            if (model.if_subscibe == 1) {
                ToatUtils.toastSuccess(getContext(), "订阅成功");
            } else {
                ToatUtils.toastSuccess(getContext(), "取消订阅");
            }
        } else {
            ToatUtils.toastCommentFail(getContext(), model.msg);
        }
    }

    public void setCaid(String caid) {
        this.caid = caid;
    }

    private String getJson() {
        SubDetailPostModel model = new SubDetailPostModel();
        model.caid = caid;
        model.page = cPage;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    void showIsLogin() {
        if (dialogLogin == null) {
            dialogLogin = new LogoutDailog(getActivity());
        }
        dialogLogin.setTextTitle("亲！你还没登录呢");
        dialogLogin.setLogoutInterface(new LogoutDailog.LogoutInterface() {
            @Override
            public void logout() {
                ActivityUtils.startLoginForResult(getActivity());
                dialogLogin.dismiss();
            }
        });
        dialogLogin.setCancelText("再去逛逛");
        dialogLogin.setOkText("去登录");
        dialogLogin.show();
    }

    void initViews() {
        rv_sub_detail = (RecyclerView) contentView.findViewById(R.id.rv_sub_detail);
        linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rv_sub_detail.setLayoutManager(linearLayoutManager);
        subDetailAdapter = new SubDetailAdapter(getActivity());
        subDetailAdapter.setEnterDetails(new SubDetailAdapter.EnterDetails() {
            @Override
            public void enterDetail(String cid) {
                ActivityUtils.startDetails(getActivity(), cid);
            }
        });
        subDetailAdapter.setSubListener(new SubDetailAdapter.SubListener() {
            @Override
            public void sub(String caid, String name, String cover_path) {
                if (SharePreferencesUtils.isLogin()) {
                    showLoading();
                    currentName = name;
                    currentPath = cover_path;
                    presenter.sub(getPostSubJson(caid));
                } else {
                    showIsLogin();
                }
            }
        });
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(subDetailAdapter);
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_sub_detail, false);
        headerViewRecyclerAdapter.addFooterView(footView);
        rv_sub_detail.setAdapter(headerViewRecyclerAdapter);
        rv_sub_detail.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                cPage = currentPage;
                presenter.loadMoreData(getJson());
            }
        });
    }

    private String getPostSubJson(String caid) {
        SubPModel pmodel = new SubPModel();
        pmodel.caid = caid;
        pmodel.uid = SharePreferencesUtils.getUid();
        pmodel.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(pmodel);
    }

    private void showLoading() {
        if (loadingDialog == null) {
            loadingDialog = new DialogSendingComment(getContext());
        }
        loadingDialog.show();
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

}
